package cn.crawler.app.crown.commons.websocket.filter;

import cn.crawler.app.crown.modules.models.CrownEquipment;
import cn.crawler.app.crown.modules.services.CrownEquipmentService;
import cn.crawler.framework.util.Result;
import cn.crawler.app.crown.commons.util.DecipherUtil;
import cn.crawler.app.crown.commons.websocket.main.action.BaseNettyAction;
import cn.crawler.app.crown.commons.websocket.main.base.NettyRouter;
import cn.crawler.app.crown.commons.websocket.main.filter.BaseNettyFilter;
import cn.crawler.app.crown.commons.websocket.main.filter.NettyFilter;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * 登录认证
 */
//支持集成|spring.@Baen
@IocBean
@NettyFilter
public class EncryptFilter extends BaseNettyFilter {

    @Reference
    @Inject
    private CrownEquipmentService service;

    @Override
    public Result doFilter(NettyRouter router, BaseNettyAction action) {
        String no = router.getParams().getString("no");
        String data = router.getParams().getString("data");
        //设备密钥
        CrownEquipment sysEquipment = service.fetchByNo(no);
        if (sysEquipment == null) {
            return Result.error();
        }
        String secret = sysEquipment.getSecret();
        //解析参数
        String params = DecipherUtil.decrypt(secret, data);
        if (params == null) {
            return Result.error();
        }
        //重置参数
        router.setParams(params);
        router.setParam("no",no);
        return null;
    }
}
